package com.shenghui.enterprise.base.controller;

import com.shenghui.enterprise.base.entity.BaseUser;
import com.shenghui.enterprise.utils.MD5;
import com.shenghui.enterprise.utils.UserUtil;
import com.shenghui.enterprise.vo.Result;
import com.shenghui.enterprise.base.entity.PageModel;
import com.shenghui.enterprise.base.service.IBaseUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Description: 用户基本信息表 前端控制器
 * @Author houjh
 * @Email: happyxiaohou@gmail.com
 * @Date: 2020-05-29
 * @Copyright (c) 2020-2022 云南昇晖科技有限公司
 */
@RestController
@RequestMapping("/admin/baseUser" )
@Api(value = "用户基本信息表接口",description = "用作用户基本信息表演示")
public class BaseUserController {
    @Autowired
    private IBaseUserService baseUserService;
    @Autowired
    private UserUtil userUtil;

    /**
     * 管理用户列表
     */
    @RequestMapping("/getBaseUserList")
    public Object getBaseUserList(@RequestParam Map map) {
        try {
            return baseUserService.getBaseUserList(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageModel(0, null);
        }
    }

    /**
     * 管理用户添加
     * @param
     * @param
     * @return
     */
    @RequestMapping("/addBaseUser")
    public Object addBaseUser(BaseUser baseUser, String id){
        try {
            return baseUserService.addBaseUser(baseUser, id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error( "保存失败");
        }
    }

    /**
     * 通过用户ID查询管理用户详情
     * @param userId
     * @return
     */
    @RequestMapping("/getBaseUserById")
    public Object getBaseUserById(String userId){
        try{
            Result result = baseUserService.getBaseUserById(userId);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error( "查询失败");
        }
    }

    /**
     * 删除管理用户
     * @return
     */
    @RequestMapping("/deleteBaseUser")
    public Object deleteBaseUser(String ids) {
        try{
            Result result = baseUserService.deleteBaseUser(ids);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error( "删除失败");
        }
    }

    /**
     * 查询所有管理用户信息
     * @return
     */
    @RequestMapping("getBaseUserAllList")
    public Object getBaseUserAllList(){
        try {
            List<Map<String, Object>> dataMap = baseUserService.getBaseUserAllList();
            return Result.ok(dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询失败!");
    }

    /**
     * 通过ID查询用户信息
     * @param id
     * @return
     */
    @RequestMapping("/getBaseUserSingleById")
    public Object getBaseUserSingleById(String id){
        try {
            BaseUser user = baseUserService.getBaseUserSingleById(id);
            return Result.ok("获取数据成功!", user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("获取书籍失败!");
    }

    /**
     * 修改密码
     * @param map
     * @return
     */
    @RequestMapping("/updataPass")
    public Object updatePass(@RequestParam Map map){
        BaseUser sessionUser = userUtil.getLoginUser();
        if (StringUtils.isEmpty(sessionUser)) {
            return Result.error("用户未登录");
        }
        String oldPass = map.get("oldPass").toString().trim();
        String newPass = map.get("newPass").toString().trim();
        String confirmPass = map.get("confirmPass").toString().trim();
        BaseUser queryedUser = baseUserService.getBaseUserSingleById(sessionUser.getUserId());
        if (StringUtils.isEmpty(queryedUser)) {
            return Result.error("修改密码出错!");
        }
        if (!(MD5.MD5Encode(oldPass).equals(queryedUser.getPassword()))) {
            return Result.error("旧密码输入不正确!");
        }
        if (!(newPass.equals(confirmPass))) {
            return Result.error("两次输入密码不一致!");
        }
        try {
            BaseUser updateUser = new BaseUser();
            updateUser.setUserId(sessionUser.getUserId());
            updateUser.setPassword(newPass);
            return baseUserService.updataPass(updateUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("密码修改失败!");
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
    @RequestMapping("/getCurrentBaseUser")
    public Object getCurrentBaseUser() {
        BaseUser baseUser = userUtil.getLoginUser();
        if(!StringUtils.isEmpty(baseUser)){
            baseUser.setPassword("");
            return Result.ok(baseUser);
        } else {
            return Result.error("获取当前登录用户失败!");
        }
    }
}
